/*
 * 公共函数文件 
 */

/**
 * 异步删除函数
 * @param {Object} module
 * @param {Object} method
 * @param {Object} data
 */

var deleteAction = function(module, data){
 		
 	var url = hjjp_rootUrl + "/" + module + "/" + "delete";
 	
 	$.post(url, data, function(resp){
		if(resp.status === 200){
			window.setTimeout(window.location.reload(), 1000);
		}else{
			$.scojs_message(resp.errorMsg, $.scojs_message.TYPE_ERROR);
		}
	}, "json");
};

/**
 * select2的tag样式 
 * @param {Object} tagList id
 * @param {Object} tagId
 */
var handleTag = function(tagList, tagId){
   	$tag = new Array();
   	$("." + tagList).each(function(){
   		$tag.push($(this).val());
   	});
   	$("#" + tagId).select2({
        tags: $tag
    });
};

/**
 *添加badge
 */
function addBadge(obj, badgeContainer, name){
	var selected = $(obj).val();
	if(selected === "-1"){
		$.scojs_message("请选择选项！", $.scojs_message.TYPE_ERROR);
		return false;
	}
	var flag = true;
	$(badgeContainer).find("input").each(function(){//是否已选择分类
		if(selected === $(this).val()){
			$.scojs_message("该选项已经选择！", $.scojs_message.TYPE_ERROR);
			flag = false;
			return false;
		}
	});
	if(flag === false){
		return false;
	}
	var html = "<div class='badge badge-large'><span>" + $(obj + " option:selected").text() +
				"</span><input type='hidden' name='" + name + "Belongs[]' value='" + selected +"' /><button type='button' class='close cred badge-close'></button></div>";
	$(badgeContainer).append(html);
	return;
}

/**
 * 后台添加badge
 */
function addBadgeBackground(obj, badgeContainer, name){
	var html = "";
	$(badgeContainer).html("");
	$(obj).each(function(){
		html += "<div class='badge badge-large'><span>" + $(this).val() +
				"</span><input type='hidden' name='" + name + "Belongs[]' value='" + $(this).attr("data-" + name) +"' /><button type='button' class='close cred badge-close'></button></div>";
	});
	$(badgeContainer).append(html);
	return;
}


/**
 * 后台添加extra
 */
function addExtraBackground(obj, container){
	var html = "";
	$(container).html("");
	$(obj).each(function(){
		html += '<tr><td><p class="cred">' + $(this).attr("data-name") +
							'</p><input type="hidden" class="extraId" value="' + $(this).attr("data-extra") +
							'" /></td><td><input type="hidden" name="price['+ $(this).attr("data-extra") +
							']" value="' + $(this).val() + 
							'" /><p>' + $(this).val() + '</p></td><td>' +
							'<a href="javascript:;" class="btn blue" ><i class="icon-pencil"></i>  编辑</a>&nbsp;&nbsp;' +
							'<a href="javascript:;" class="btn red" ><i class="icon-minus"></i>  删除</a>' +
							'</td></tr>';
	});
	$(container).append(html);
	return;
}

/**
 *添加badge
 */
function removeBadge(obj){
	$(obj).parent().remove();
	return;
}

/**
 *检查扩展属性类型 
 */
function checkExtraType(obj, hideLayer){
	var selected = $(obj).find("option:selected").attr("data-type");
	if(selected == 'size'){
		$(hideLayer).parent().parent().removeClass("hide");
		$(hideLayer).attr("required", "required");
	}else{
		$(hideLayer).parent().parent().addClass("hide");
		$(hideLayer).val("");
		$(hideLayer).removeAttr("required");
	}
}

/**
 *获取满足条件的店铺
 */
function getStoreBelongs(rid, tid, obj){
	var url = hjjp_rootUrl + "/Statistic/storesjson";
	$.post(url, {storetype: tid, region: rid}, function(resp){
		if(resp.status == 200){
			$(obj).html(resp.data);
		}
	}, 'json');
}